Repository navigation
Fix issues #129, #130, #94, #106, #105, #126 - #137
Snehil208001 wants to merge 3 commits into
Conversation
…a17#106, jaygajera17#105, jaygajera17#126 - jaygajera17#129: Add delete/toggleActive functionality for customers (AdminCustomerController) - jaygajera17#130: Add isActive boolean flag in User model with soft delete and security checks - jaygajera17#94: Fix Update Product Details (AdminProductController, productsUpdate.jsp) - jaygajera17#106: Fix misaligned login button on mobile (adminlogin.jsp CSS) - jaygajera17#105: Add Dockerfile and docker-compose.yml for easier setup - jaygajera17#126: Split Admin controller into AdminController, AdminCategoryController, AdminProductController, AdminCustomerController Additional: AdminDataLoader for BCrypt admin password, fix SecurityConfiguration UserDetailsService Made-with: Cursor
ec40169 to
2fda2e0
Compare
Testing instructionsSetup:
Admin login: Docker: Verified: All Maven tests pass locally. |
Key files changed
|
|
Thanks @Snehil208001 , I'll check this |
There was a problem hiding this comment.
Pull request overview
This PR addresses several admin-panel and infrastructure issues by introducing customer soft-deactivation (isActive), splitting the monolithic admin controller into focused controllers, fixing product update UI behavior, improving the admin login mobile layout, and adding Docker/Docker Compose setup for easier local deployment.
Changes:
- Add customer
isActiveflag with admin toggle/deactivation endpoints and active-account enforcement during authentication. - Split admin functionality into
AdminController,AdminCategoryController,AdminProductController, andAdminCustomerController. - Add Dockerfile + docker-compose for running the app with MySQL, and adjust SQL seed/config to support BCrypt admin login.
Reviewed changes
Copilot reviewed 20 out of 21 changed files in this pull request and generated 19 comments.
Show a summary per file
| File | Description |
|---|---|
| docker-compose.yml | Adds MySQL + app services for containerized local runs. |
| JtProject/Dockerfile | Adds multi-stage Maven build and runtime image. |
| JtProject/.dockerignore | Reduces Docker build context size. |
| JtProject/src/main/resources/application.properties | Adjusts Hibernate/db properties and package scanning. |
| JtProject/basedata.sql | Updates schema/seed (DB name, is_active, BCrypt admin password). |
| JtProject/insert_admin.sql | Adds a standalone admin insert script. |
| JtProject/fix_admin_password.sql | Adds a standalone admin BCrypt password fix script. |
| JtProject/src/main/java/.../models/User.java | Introduces isActive column/field. |
| JtProject/src/main/java/.../dao/userDao.java | Adds soft delete/toggle/get-by-id DAO operations. |
| JtProject/src/main/java/.../services/userService.java | Exposes new user management service methods. |
| JtProject/src/main/java/.../configuration/SecurityConfiguration.java | Permits admin login processing URL and blocks inactive users. |
| JtProject/src/main/java/.../config/AdminDataLoader.java | Attempts to enforce BCrypt admin password on startup. |
| JtProject/src/main/java/.../HibernateConfiguration.java | Adds an entityManagerFactory bean for JPA integration. |
| JtProject/src/main/java/.../controller/AdminController.java | Removes direct JDBC usage; uses service + encoder for profile updates. |
| JtProject/src/main/java/.../controller/AdminCategoryController.java | Extracts category admin endpoints. |
| JtProject/src/main/java/.../controller/AdminProductController.java | Extracts product admin endpoints + fixes update flow. |
| JtProject/src/main/java/.../controller/AdminCustomerController.java | Extracts customer admin endpoints (toggle/deactivate). |
| JtProject/src/main/webapp/views/productsUpdate.jsp | Fixes product update form bindings (category select, description, image). |
| JtProject/src/main/webapp/views/displayCustomers.jsp | Adds Active/Inactive display and Activate/Deactivate action. |
| JtProject/src/main/webapp/views/adminlogin.jsp | CSS tweaks to improve mobile layout. |
| .gitignore | Adds cookies.txt to ignored files. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @GetMapping("customers/delete") | ||
| public String deleteCustomer(@RequestParam("id") int id) { | ||
| this.userService.deleteUser(id); | ||
| return "redirect:/admin/customers"; | ||
| } |
There was a problem hiding this comment.
This endpoint changes customer state via GET. With CSRF disabled, this is CSRF-able and can be triggered unintentionally (prefetch/crawlers). Prefer POST/DELETE for non-idempotent actions and re-enable CSRF protection.
| <th scope="col">Status</th> | ||
| <th scope="col">Actions</th> | ||
| </tr> |
There was a problem hiding this comment.
The PR description/issue #129 mentions adding delete functionality for customers, but the UI now only provides Activate/Deactivate and no Delete action. If “delete” is intended to be a soft delete, consider labeling it accordingly (Deactivate) and remove the unused /customers/delete endpoint; otherwise, add the missing Delete action to the table.
| ports: | ||
| - "3306:3306" | ||
| volumes: | ||
| - mysql_data:/var/lib/mysql |
There was a problem hiding this comment.
Compose doesn’t mount any init SQL into the MySQL container (e.g., /docker-entrypoint-initdb.d). As a result, a fresh Docker run won’t have base tables/admin user unless Hibernate creates them and you seed data manually. Consider mounting basedata.sql (and/or an admin seed) as an init script volume.
| - mysql_data:/var/lib/mysql | |
| - mysql_data:/var/lib/mysql | |
| - ./basedata.sql:/docker-entrypoint-initdb.d/basedata.sql:ro |
| import java.util.Properties; | ||
|
|
||
| import javax.sql.DataSource; | ||
| import javax.persistence.EntityManagerFactory; |
There was a problem hiding this comment.
import javax.persistence.EntityManagerFactory; is added but not used in this class. Removing unused imports keeps the configuration class easier to maintain and avoids confusion about which persistence API is intended here.
| import javax.persistence.EntityManagerFactory; |
| @GetMapping("customers/toggleActive") | ||
| public String toggleCustomerActive(@RequestParam("id") int id) { | ||
| this.userService.toggleUserActive(id); | ||
| return "redirect:/admin/customers"; |
There was a problem hiding this comment.
This endpoint changes customer state via GET. With CSRF disabled, this is CSRF-able and can be triggered unintentionally (prefetch/crawlers). Prefer POST/PATCH for non-idempotent actions and re-enable CSRF protection.
| # insert default customers (BCrypt: admin=123) | ||
| # For lisa=765, register via /register or update password in DB with BCrypt hash | ||
| INSERT INTO CUSTOMER(address, email, password, role, username, is_active) VALUES | ||
| ('123, Albany Street', 'admin@nyan.cat', '$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG', 'ROLE_ADMIN', 'admin', 1), | ||
| ('765, 5th Avenue', 'lisa@gmail.com', '765', 'ROLE_NORMAL', 'lisa', 1); |
There was a problem hiding this comment.
The default user lisa is inserted with a plain-text password ('765'), but authentication now uses BCryptPasswordEncoder, which will reject non-BCrypt stored values and break login. Store a BCrypt hash for lisa too (or remove the credential from the default dataset/docs).
| # insert default customers (BCrypt: admin=123) | |
| # For lisa=765, register via /register or update password in DB with BCrypt hash | |
| INSERT INTO CUSTOMER(address, email, password, role, username, is_active) VALUES | |
| ('123, Albany Street', 'admin@nyan.cat', '$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG', 'ROLE_ADMIN', 'admin', 1), | |
| ('765, 5th Avenue', 'lisa@gmail.com', '765', 'ROLE_NORMAL', 'lisa', 1); | |
| # insert default customers (BCrypt: admin=123, lisa=765) | |
| # Both default customer passwords are stored as BCrypt hashes for compatibility with BCryptPasswordEncoder | |
| INSERT INTO CUSTOMER(address, email, password, role, username, is_active) VALUES | |
| ('123, Albany Street', 'admin@nyan.cat', '$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG', 'ROLE_ADMIN', 'admin', 1), | |
| ('765, 5th Avenue', 'lisa@gmail.com', '$2a$10$7EqJtq98hPqEX7fNZaFWoOHiWqD3oB8LE6wS6vKrrf5wM9vY7Gk8W', 'ROLE_NORMAL', 'lisa', 1); |
| User admin = userService.getUserByUsername("admin"); | ||
| if (admin != null && !passwordEncoder.matches("123", admin.getPassword())) { | ||
| admin.setPassword(passwordEncoder.encode("123")); | ||
| admin.setActive(true); |
There was a problem hiding this comment.
Hardcoding the admin username/password here (admin / 123) effectively resets credentials at startup and is unsafe outside local/dev usage. Gate this behind an explicit property/profile and source the password from configuration rather than embedding it in code.
| @Override | ||
| public void run(String... args) { | ||
| try { | ||
| User admin = userService.getUserByUsername("admin"); | ||
| if (admin != null && !passwordEncoder.matches("123", admin.getPassword())) { | ||
| admin.setPassword(passwordEncoder.encode("123")); | ||
| admin.setActive(true); | ||
| userService.updateUser(admin); | ||
| System.out.println(">>> Admin password updated. Login with admin/123"); |
There was a problem hiding this comment.
passwordEncoder.matches("123", admin.getPassword()) can throw if the stored password is not a BCrypt hash (e.g., legacy plain-text), and the catch will skip fixing the admin account. Handle non-BCrypt stored values deterministically (e.g., detect hash prefix and force-encode) rather than relying on matches alone.
| @Override | |
| public void run(String... args) { | |
| try { | |
| User admin = userService.getUserByUsername("admin"); | |
| if (admin != null && !passwordEncoder.matches("123", admin.getPassword())) { | |
| admin.setPassword(passwordEncoder.encode("123")); | |
| admin.setActive(true); | |
| userService.updateUser(admin); | |
| System.out.println(">>> Admin password updated. Login with admin/123"); | |
| private boolean isBcryptHash(String password) { | |
| return password != null | |
| && (password.startsWith("$2a$") | |
| || password.startsWith("$2b$") | |
| || password.startsWith("$2y$")); | |
| } | |
| @Override | |
| public void run(String... args) { | |
| try { | |
| User admin = userService.getUserByUsername("admin"); | |
| if (admin != null) { | |
| String storedPassword = admin.getPassword(); | |
| boolean passwordMatches = isBcryptHash(storedPassword) | |
| && passwordEncoder.matches("123", storedPassword); | |
| if (!passwordMatches) { | |
| admin.setPassword(passwordEncoder.encode("123")); | |
| admin.setActive(true); | |
| userService.updateUser(admin); | |
| System.out.println(">>> Admin password updated. Login with admin/123"); | |
| } |
| model.addAttribute("userid", user.getId()); | ||
| model.addAttribute("username", user.getUsername()); | ||
| model.addAttribute("email", user.getEmail()); | ||
| model.addAttribute("password", user.getPassword()); | ||
| model.addAttribute("address", user.getAddress()); |
There was a problem hiding this comment.
profileDisplay is putting the stored password hash into the model (password), and updateProfile.jsp uses it to pre-fill the password field. Submitting the form will re-encode an already-encoded value and effectively change the password (locking the user out). Don’t send any password (even hashed) to the view; only encode/update when the user explicitly provides a new password.
|
@Snehil208001 can you please change the base branch to master |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Additional: AdminDataLoader for BCrypt admin password, fix SecurityConfiguration UserDetailsService